package com.qianbao.qianbaobusiness.ui.invoice;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baseandroid.config.Constant;
import com.baseandroid.event.EInvoiceListEvent;
import com.baseandroid.mvp.BaseMvpActivity;
import com.baseandroid.utils.MoneyUtil;
import com.baseandroid.utils.TimeUtils;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.EInvoiceDetailBean;
import com.qianbao.qianbaobusiness.ui.invoice.dialog.InvalidInvoiceDialog;
import com.qianbao.qianbaobusiness.ui.widget.dialog.ConfirmDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class EInvoiceDetailActivity extends BaseMvpActivity<EInvoiceDetailView, EInvoiceDetailPresenter> implements EInvoiceDetailView {

    @BindView(R.id.tv_title)
    TextView  mTvTitle;
    @BindView(R.id.tv_right_click)
    TextView  mTvRightClick;
    @BindView(R.id.iv_state)
    ImageView mIvState;
    @BindView(R.id.tv_state)
    TextView  mTvState;
    @BindView(R.id.tv_title_invoice)
    TextView  mTvTitleInvoice;
    @BindView(R.id.tv_enterprise)
    TextView  mTvEnterprise;
    @BindView(R.id.tv_date)
    TextView  mTvDate;
    @BindView(R.id.tv_money)
    TextView  mTvMoney;
    @BindView(R.id.tv_order_no)
    TextView  mTvOrderNo;
    @BindView(R.id.tv_tel)
    TextView  mTvTel;
    @BindView(R.id.tv_email)
    TextView  mTvEmail;
    private String               mInvoiceId;
    private ConfirmDialog        mDialog;
    private InvalidInvoiceDialog mInvalidDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_einvoice_detail;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        initBundle();
        mTvTitle.setText("电子发票详情");
        mTvRightClick.setText("作废发票");
        initData();
    }

    private void initData() {
        getPresenter().getInvoiceDetail(mInvoiceId);
    }

    private void startInvalidInvoice() {
        getPresenter().invalidInvoice(mInvoiceId);
        mInvalidDialog = new InvalidInvoiceDialog(this);
        mInvalidDialog.show();
        mInvalidDialog.setState(InvalidInvoiceDialog.INVALID_PROCESS_STATE);
        mInvalidDialog.setClickListener(new InvalidInvoiceDialog.ConfirmClickListener() {
            @Override
            public void click(int state) {
                if (state == InvalidInvoiceDialog.INVALID_FAIL_STATE) {
                    mInvalidDialog.dismiss();
                } else if (state == InvalidInvoiceDialog.INVALID_SUCCESS_STATE) {
                    mInvalidDialog.dismiss();
                    mTvRightClick.setVisibility(View.GONE);
                    mIvState.setImageResource(R.drawable.icon_transaction_detail_exception);
                    mTvState.setText("已作废");
                    EventBus.getDefault().post(new EInvoiceListEvent(mInvoiceId));
                }
            }
        });
    }

    private void initBundle() {
        Intent intent = getIntent();
        Bundle extras = intent.getBundleExtra("bundle");
        mInvoiceId = extras != null ? extras.getString("invoiceId", "") : "";
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick({R.id.iv_back_toolbar, R.id.tv_right_click})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back_toolbar:
                finish();
                break;
            case R.id.tv_right_click:
                ConfirmDialog.Builder builder = new ConfirmDialog.Builder(EInvoiceDetailActivity.this);
                mDialog = builder.setContentText("确认作废该笔发票吗？\r\n一旦确认不可取消。")
                                  .setCanceledOnTouchOutside(false)
                                  .setNegativeText("取消")
                                  .setPositiveText("确认")
                                  .setConfirmListener(new ConfirmDialog.Builder.ConfirmClickListener() {
                                      @Override
                                      public void click(boolean b) {
                                          if (!b) {
                                              mDialog.dismiss();
                                          } else {
                                              startInvalidInvoice();
                                              mDialog.dismiss();
                                          }
                                      }
                                  }).create();
                mDialog.show();
                break;
            default:
                break;
        }
    }

    @Override
    public void getInvoiceDetailSuccess(EInvoiceDetailBean result) {
        EInvoiceDetailBean.InvoiceDTOBean bean = result.getInvoiceDTO();
        switch (bean.getStatus()) {
            case "1":
                mIvState.setImageResource(R.drawable.icon_transaction_detail_success);
                mTvState.setText("开票成功");
                break;
            case "2":
                mTvState.setText("开票失败");
                mIvState.setImageResource(R.drawable.icon_transaction_detail_fail);
                break;
            case "3":
                mTvState.setText("已作废");
                mIvState.setImageResource(R.drawable.icon_transaction_detail_exception);
                break;
            default:
                break;
        }
        if (bean.getIsEnbaleCode().equals("true")) {
            mTvRightClick.setVisibility(View.VISIBLE);
        } else {
            mTvRightClick.setVisibility(View.GONE);
        }
        mTvTitleInvoice.setText(bean.getRiseName());
        mTvEnterprise.setText(bean.getMerchantRegisterName());
        mTvDate.setText(TimeUtils.getDate(bean.getInvoiceDate()
                , Constant.DATE_YYYY_MM_DD_HH_MM_SS, Constant.DATE_YYYY_MM_DD));
        String money = MoneyUtil.parseMoney(bean.getInvoiceAmount());
        mTvMoney.setText(money + "元");
        mTvOrderNo.setText(bean.getOrderNo());
        String mobile = bean.getMobile();
        mobile = mobile.replaceAll("(\\d{3})(\\d+)(\\d{4})", "$1****$3");
        mTvTel.setText(mobile);
        mTvEmail.setText(bean.getEmail());
    }

    @Override
    public void invalidInvoiceSuccess() {
        if (mInvalidDialog != null) {
            mInvalidDialog.setState(InvalidInvoiceDialog.INVALID_SUCCESS_STATE);
        }
    }

    @Override
    public void invalidInvoiceFail() {
        if (mInvalidDialog != null) {
            mInvalidDialog.setState(InvalidInvoiceDialog.INVALID_FAIL_STATE);
        }
    }
}
